﻿using System;
using System.Collections.Generic;
using System.Linq;
using IllusionUtility.GetUtility;
using UniRx;
using UniRx.Triggers;
using UnityEngine;

namespace Studio
{
	// Token: 0x02000457 RID: 1111
	public class OptionItemCtrl : MonoBehaviour
	{
		// Token: 0x170002BC RID: 700
		// (get) Token: 0x06001BA7 RID: 7079 RVA: 0x000B199C File Offset: 0x000AFB9C
		// (set) Token: 0x06001BA8 RID: 7080 RVA: 0x000B19C8 File Offset: 0x000AFBC8
		public Animator animator
		{
			get
			{
				if (this.m_Animator == null)
				{
					this.m_Animator = base.gameObject.GetComponentInChildren<Animator>();
				}
				return this.m_Animator;
			}
			set
			{
				this.m_Animator = value;
			}
		}

		// Token: 0x170002BD RID: 701
		// (get) Token: 0x06001BA9 RID: 7081 RVA: 0x000B19D4 File Offset: 0x000AFBD4
		// (set) Token: 0x06001BAA RID: 7082 RVA: 0x000B19DC File Offset: 0x000AFBDC
		public OICharInfo oiCharInfo { get; set; }

		// Token: 0x170002BE RID: 702
		// (get) Token: 0x06001BAB RID: 7083 RVA: 0x000B19E8 File Offset: 0x000AFBE8
		// (set) Token: 0x06001BAC RID: 7084 RVA: 0x000B19F0 File Offset: 0x000AFBF0
		private int count { get; set; }

		// Token: 0x170002BF RID: 703
		// (get) Token: 0x06001BAD RID: 7085 RVA: 0x000B19FC File Offset: 0x000AFBFC
		// (set) Token: 0x06001BAE RID: 7086 RVA: 0x000B1A0C File Offset: 0x000AFC0C
		public bool visible
		{
			get
			{
				return this.oiCharInfo.animeOptionVisible;
			}
			set
			{
				this.oiCharInfo.animeOptionVisible = value;
				this.SetVisible(this.m_OutsideVisible & this.oiCharInfo.animeOptionVisible);
			}
		}

		// Token: 0x170002C0 RID: 704
		// (get) Token: 0x06001BAF RID: 7087 RVA: 0x000B1A34 File Offset: 0x000AFC34
		// (set) Token: 0x06001BB0 RID: 7088 RVA: 0x000B1A3C File Offset: 0x000AFC3C
		public bool outsideVisible
		{
			get
			{
				return this.m_OutsideVisible;
			}
			set
			{
				this.m_OutsideVisible = value;
				this.SetVisible(this.m_OutsideVisible & this.oiCharInfo.animeOptionVisible);
			}
		}

		// Token: 0x170002C1 RID: 705
		// (set) Token: 0x06001BB1 RID: 7089 RVA: 0x000B1A60 File Offset: 0x000AFC60
		public float height
		{
			set
			{
				foreach (OptionItemCtrl.ItemInfo itemInfo in this.hashItem)
				{
					itemInfo.height = value;
				}
			}
		}

		// Token: 0x06001BB2 RID: 7090 RVA: 0x000B1ABC File Offset: 0x000AFCBC
		public void LoadAnimeItem(Info.AnimeLoadInfo _info, string _clip, float _height, float _motion)
		{
			this.ReleaseAllItem();
			int i = 0;
			while (i < _info.listOptions.Count)
			{
				Info.OptionItemInfo optionItemInfo = _info.listOptions[i];
				Info.ItemLoadInfo itemLoadInfo = null;
				if (_info.isHAnime)
				{
					if (Singleton<Info>.Instance.dicHItemLoadInfo.TryGetValue(optionItemInfo.no, out itemLoadInfo))
					{
						goto IL_88;
					}
					GlobalData.Debug_Log(string.Format("失敗 : [{0}]", optionItemInfo.no));
				}
				else if (Singleton<Info>.Instance.dicItemLoadInfo.TryGetValue(optionItemInfo.no, out itemLoadInfo))
				{
					goto IL_88;
				}
				IL_32F:
				i++;
				continue;
				IL_88:
				GameObject gameObject = Utility.LoadAsset<GameObject>(itemLoadInfo.bundlePath, itemLoadInfo.fileName, itemLoadInfo.manifest);
				if (gameObject == null)
				{
					goto IL_32F;
				}
				OptionItemCtrl.ItemInfo itemInfo = new OptionItemCtrl.ItemInfo(_height);
				itemInfo.gameObject = gameObject;
				itemInfo.scale = gameObject.transform.localScale;
				itemInfo.isHAnime = _info.isHAnime;
				itemInfo.isScale = _info.isScale;
				itemInfo.min = _info.scale[0];
				itemInfo.max = _info.scale[1];
				itemInfo.isMotion = _info.isMotion;
				itemInfo.animator = gameObject.GetComponent<Animator>();
				if (itemInfo.animator)
				{
					if (!optionItemInfo.bundlePath.IsNullOrEmpty() && !optionItemInfo.fileName.IsNullOrEmpty())
					{
						RuntimeAnimatorController runtimeAnimatorController = CommonLib.LoadAsset<RuntimeAnimatorController>(optionItemInfo.bundlePath, optionItemInfo.fileName, false, string.Empty);
						if (runtimeAnimatorController != null)
						{
							itemInfo.animator.runtimeAnimatorController = runtimeAnimatorController;
						}
						AssetBundleManager.UnloadAssetBundle(optionItemInfo.bundlePath, true, null, false);
						itemInfo.animator.Play(_clip);
						if (_info.isHAnime)
						{
							itemInfo.animator.SetFloat("height", _height);
							if (_info.isMotion)
							{
								itemInfo.animator.SetFloat("motion", _motion);
							}
						}
						itemInfo.isSync = true;
					}
					else
					{
						itemInfo.isSync = false;
					}
				}
				for (int j = 0; j < optionItemInfo.parentageInfo.Length; j++)
				{
					GameObject gameObject2 = base.gameObject.transform.FindLoop(optionItemInfo.parentageInfo[j].parent);
					GameObject gameObject3 = gameObject;
					if (!optionItemInfo.parentageInfo[j].child.IsNullOrEmpty())
					{
						gameObject3 = gameObject3.transform.FindLoop(optionItemInfo.parentageInfo[j].child);
						itemInfo.child.Add(new OptionItemCtrl.ChildInfo(gameObject3.transform.localScale, gameObject3));
					}
					gameObject3.transform.SetParent(gameObject2.transform);
					gameObject3.transform.localPosition = Vector3.zero;
					gameObject3.transform.localRotation = Quaternion.identity;
					gameObject3.transform.localScale = ((!_info.isScale) ? itemInfo.scale : Vector3.Lerp(_info.scale[0], _info.scale[1], _height));
				}
				itemInfo.SetRender();
				this.hashItem.Add(itemInfo);
				goto IL_32F;
			}
			this.count = this.hashItem.Count;
			this.SetVisible(this.visible);
		}

		// Token: 0x06001BB3 RID: 7091 RVA: 0x000B1E2C File Offset: 0x000B002C
		public void ReleaseAllItem()
		{
			foreach (OptionItemCtrl.ItemInfo itemInfo in from i in this.hashItem
			where i != null
			select i)
			{
				itemInfo.Release();
			}
			this.hashItem.Clear();
			this.count = 0;
		}

		// Token: 0x06001BB4 RID: 7092 RVA: 0x000B1EB8 File Offset: 0x000B00B8
		public void SetMotion(float _motion)
		{
			foreach (OptionItemCtrl.ItemInfo itemInfo in from i in this.hashItem
			where i.animator && i.isMotion && i.isSync
			select i)
			{
				itemInfo.animator.SetFloat("motion", _motion);
			}
		}

		// Token: 0x06001BB5 RID: 7093 RVA: 0x000B1F40 File Offset: 0x000B0140
		public void ChangeScale(Vector3 _value)
		{
			foreach (OptionItemCtrl.ItemInfo itemInfo in this.hashItem)
			{
				Transform transform = itemInfo.gameObject.transform;
				transform.localScale = ((!itemInfo.isScale) ? itemInfo.scale : Vector3.Lerp(itemInfo.min, itemInfo.max, itemInfo.height));
			}
		}

		// Token: 0x06001BB6 RID: 7094 RVA: 0x000B1FD4 File Offset: 0x000B01D4
		private void SetVisible(bool _visible)
		{
			foreach (OptionItemCtrl.ItemInfo itemInfo in this.hashItem)
			{
				itemInfo.active = _visible;
			}
		}

		// Token: 0x06001BB7 RID: 7095 RVA: 0x000B2030 File Offset: 0x000B0230
		private void Awake()
		{
			this.m_OutsideVisible = true;
			(from _ in this.LateUpdateAsObservable()
			where this.animator != null && this.count != 0
			select this.animator.GetCurrentAnimatorStateInfo(0)).Subscribe(delegate(AnimatorStateInfo info)
			{
				foreach (OptionItemCtrl.ItemInfo itemInfo in this.hashItem)
				{
					if (itemInfo.animator && itemInfo.isSync)
					{
						itemInfo.animator.Play(info.fullPathHash, 0, info.normalizedTime);
					}
				}
			}).AddTo(this);
		}

		// Token: 0x04001F03 RID: 7939
		private Animator m_Animator;

		// Token: 0x04001F05 RID: 7941
		private HashSet<OptionItemCtrl.ItemInfo> hashItem = new HashSet<OptionItemCtrl.ItemInfo>();

		// Token: 0x04001F07 RID: 7943
		private bool m_OutsideVisible = true;

		// Token: 0x02000458 RID: 1112
		private class ChildInfo
		{
			// Token: 0x06001BBD RID: 7101 RVA: 0x000B2178 File Offset: 0x000B0378
			public ChildInfo(Vector3 _scale, GameObject _obj)
			{
				this.scale = _scale;
				this.obj = _obj;
			}

			// Token: 0x04001F0A RID: 7946
			public Vector3 scale = Vector3.one;

			// Token: 0x04001F0B RID: 7947
			public GameObject obj;
		}

		// Token: 0x02000459 RID: 1113
		private class ItemInfo
		{
			// Token: 0x06001BBE RID: 7102 RVA: 0x000B219C File Offset: 0x000B039C
			public ItemInfo(float _height)
			{
				this.m_Height = _height;
			}

			// Token: 0x170002C2 RID: 706
			// (get) Token: 0x06001BBF RID: 7103 RVA: 0x000B21C8 File Offset: 0x000B03C8
			// (set) Token: 0x06001BC0 RID: 7104 RVA: 0x000B21D0 File Offset: 0x000B03D0
			public bool isHAnime { get; set; }

			// Token: 0x170002C3 RID: 707
			// (get) Token: 0x06001BC1 RID: 7105 RVA: 0x000B21DC File Offset: 0x000B03DC
			// (set) Token: 0x06001BC2 RID: 7106 RVA: 0x000B21E4 File Offset: 0x000B03E4
			public bool isScale { get; set; }

			// Token: 0x170002C4 RID: 708
			// (get) Token: 0x06001BC3 RID: 7107 RVA: 0x000B21F0 File Offset: 0x000B03F0
			// (set) Token: 0x06001BC4 RID: 7108 RVA: 0x000B21F8 File Offset: 0x000B03F8
			public Vector3 min { get; set; }

			// Token: 0x170002C5 RID: 709
			// (get) Token: 0x06001BC5 RID: 7109 RVA: 0x000B2204 File Offset: 0x000B0404
			// (set) Token: 0x06001BC6 RID: 7110 RVA: 0x000B220C File Offset: 0x000B040C
			public Vector3 max { get; set; }

			// Token: 0x170002C6 RID: 710
			// (get) Token: 0x06001BC7 RID: 7111 RVA: 0x000B2218 File Offset: 0x000B0418
			// (set) Token: 0x06001BC8 RID: 7112 RVA: 0x000B2220 File Offset: 0x000B0420
			public Vector3 scale { get; set; }

			// Token: 0x170002C7 RID: 711
			// (get) Token: 0x06001BC9 RID: 7113 RVA: 0x000B222C File Offset: 0x000B042C
			// (set) Token: 0x06001BCA RID: 7114 RVA: 0x000B2234 File Offset: 0x000B0434
			public bool isMotion { get; set; }

			// Token: 0x170002C8 RID: 712
			// (get) Token: 0x06001BCB RID: 7115 RVA: 0x000B2240 File Offset: 0x000B0440
			// (set) Token: 0x06001BCC RID: 7116 RVA: 0x000B2248 File Offset: 0x000B0448
			public bool isSync { get; set; }

			// Token: 0x170002C9 RID: 713
			// (get) Token: 0x06001BCD RID: 7117 RVA: 0x000B2254 File Offset: 0x000B0454
			// (set) Token: 0x06001BCE RID: 7118 RVA: 0x000B225C File Offset: 0x000B045C
			public float height
			{
				get
				{
					return this.m_Height;
				}
				set
				{
					this.m_Height = value;
					if (this.animator && this.isHAnime)
					{
						this.animator.SetFloat("height", value);
					}
					if (this.isScale)
					{
						foreach (OptionItemCtrl.ChildInfo childInfo in this.child)
						{
							childInfo.obj.transform.localScale = Vector3.Lerp(this.min, this.max, this.height);
						}
					}
				}
			}

			// Token: 0x170002CA RID: 714
			// (get) Token: 0x06001BCF RID: 7119 RVA: 0x000B2318 File Offset: 0x000B0518
			// (set) Token: 0x06001BD0 RID: 7120 RVA: 0x000B2320 File Offset: 0x000B0520
			public bool active
			{
				get
				{
					return this.m_Active;
				}
				set
				{
					if (this.m_Active != value)
					{
						this.m_Active = value;
						for (int i = 0; i < this.renderer.Length; i++)
						{
							this.renderer[i].enabled = value;
						}
					}
				}
			}

			// Token: 0x06001BD1 RID: 7121 RVA: 0x000B2368 File Offset: 0x000B0568
			public void Release()
			{
				UnityEngine.Object.DestroyImmediate(this.gameObject);
				foreach (OptionItemCtrl.ChildInfo childInfo in this.child)
				{
					UnityEngine.Object.DestroyImmediate(childInfo.obj);
				}
			}

			// Token: 0x06001BD2 RID: 7122 RVA: 0x000B23D4 File Offset: 0x000B05D4
			public void SetRender()
			{
				List<Renderer> list = new List<Renderer>();
				Renderer[] componentsInChildren = this.gameObject.GetComponentsInChildren<Renderer>();
				if (!componentsInChildren.IsNullOrEmpty<Renderer>())
				{
					list.AddRange(componentsInChildren);
				}
				foreach (OptionItemCtrl.ChildInfo childInfo in this.child)
				{
					componentsInChildren = childInfo.obj.GetComponentsInChildren<Renderer>();
					if (!componentsInChildren.IsNullOrEmpty<Renderer>())
					{
						list.AddRange(componentsInChildren);
					}
				}
				this.renderer = list.ToArray();
			}

			// Token: 0x04001F0C RID: 7948
			public GameObject gameObject;

			// Token: 0x04001F0D RID: 7949
			public Animator animator;

			// Token: 0x04001F0E RID: 7950
			public List<OptionItemCtrl.ChildInfo> child = new List<OptionItemCtrl.ChildInfo>();

			// Token: 0x04001F0F RID: 7951
			private Renderer[] renderer;

			// Token: 0x04001F17 RID: 7959
			public float m_Height = 0.5f;

			// Token: 0x04001F18 RID: 7960
			private bool m_Active = true;
		}
	}
}
